package com.springboot.controller;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springboot.common.R;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import com.springboot.utils.SMSUtils;
import com.springboot.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送验证码短信
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
         //获取手机号
        String phone = user.getPhone();
        //判断手机号是否合法
        if ((StringUtils.isNotEmpty(phone))){
            //生成随机四位验证码
            String msgcode = ValidateCodeUtils.generateValidateCode(6).toString();
            log.info("code:{}",msgcode);
            //调用阿里云提供的短信服务API发送验证码短信
           SMSUtils.sendMessage("棱棱外卖","SMS_465921300",phone,msgcode);
            //保存验证码到Session
            //session.setAttribute(phone,msgcode);
            //保存验证码到redis
            redisTemplate.opsForValue().set(phone,msgcode,5, TimeUnit.MINUTES);
            return R.success(user.getPhone() + "验证码发送成功.....:"+msgcode);
        }
       
     return R.error("手机号不能为空");
    }

    /**
     * 手机号登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        //获取手机号和验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
         //从session中获取验证码
        //Object codesession = session.getAttribute(phone);
       //从redis中获取验证码
        Object rediscode = redisTemplate.opsForValue().get(phone);
        //判断验证码是否正确
        if (rediscode!=null&&rediscode.equals(code)){
            //登录成功,判断是否为空
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone,phone);
            User user = userService.getOne(wrapper);
            if (user==null){
                //用户不存在，注册用户
                 user= new User();
                user.setPhone(phone);
                user.setStatus(1);
                //保存用户
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            //如果登录成功，删除redis中的验证码
            redisTemplate.delete(phone);
           return R.success(user);
        }


        return R.error("登录失败，验证码错误");
    }
}
